package com.wupo.common.base;

import com.wupo.common.http.HTTP;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.experimental.Accessors;
import lombok.extern.slf4j.Slf4j;

import java.io.Serializable;

@Slf4j
@Data
@NoArgsConstructor
@AllArgsConstructor
@Accessors(chain = true)
@ApiModel(value = "BaseResponse实例",description = "通用响应实例")
public class BaseResponseBean implements Serializable {

    //状态码
    @ApiModelProperty(value = "状态码")
    private Integer code;

    //消息提示
    @ApiModelProperty(value = "消息提示")
    private String msg;

    //返回数据
    @ApiModelProperty(value = "返回数据")
    private Object data;

    public static BaseResponseBean success() {
        return success("request succeeded");
    }

    public static BaseResponseBean success(Object data) {
        return success("request succeeded", data);
    }

    public static BaseResponseBean success(String message, Object data) {
        return success(HTTP.SC_OK, message, data);
    }

    public static BaseResponseBean success(Integer code, String message, Object data) {
        return build(code, message, data);
    }

    public static BaseResponseBean error(Integer code) {
        return error(code, "request fail");
    }

    public static BaseResponseBean error(Integer code, String message) {
        return error(code, message, (Object)null);
    }

    public static BaseResponseBean error(Integer code, String message, Object data) {
        return build(code, message, data);
    }

    public static BaseResponseBean build(HTTP.Status status) {
        return build(status, (Object)null);
    }

    public static BaseResponseBean build(HTTP.Status status, Object data) {
        return build(status.value(), status.getReasonPhrase(), data);
    }

    public static BaseResponseBean build(Integer code, String message, Object data) {
        return new BaseResponseBean(code, message, data);
    }

    public static BaseResponseBean.BaseResponseBeanBuilder builder() {
        return new BaseResponseBean.BaseResponseBeanBuilder();
    }

    public static class BaseResponseBeanBuilder {
        private Integer code;
        private String message;
        private Object data;

        BaseResponseBeanBuilder() {
        }

        public BaseResponseBean.BaseResponseBeanBuilder code(Integer code) {
            this.code = code;
            return this;
        }

        public BaseResponseBean.BaseResponseBeanBuilder message(String message) {
            this.message = message;
            return this;
        }

        public BaseResponseBean.BaseResponseBeanBuilder data(Object data) {
            this.data = data;
            return this;
        }

        public BaseResponseBean build() {
            return new BaseResponseBean(this.code, this.message, this.data);
        }

    }

}
